package models

import (
	"goods_srv/basic/config"
	"time"
)

type Cabinet struct {
	Id               uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	CabinetNo        string    `gorm:"column:cabinet_no;type:varchar(30);not null;" json:"cabinet_no"`
	ChargingStandard string    `gorm:"column:charging_standard;type:varchar(50);comment:收费标准;not null;" json:"charging_standard"` // 收费标准
	AddressId        int32     `gorm:"column:address_id;type:int;comment:地址id;not null;" json:"address_id"`                       // 地址id
	SizeId           int32     `gorm:"column:size_id;type:int;comment:型号id;not null;" json:"size_id"`                             // 型号id
	Hot              int32     `gorm:"column:hot;type:int;comment:热度;default:0;" json:"hot"`                                      // 热度
	Status           int8      `gorm:"column:status;type:tinyint(1);comment:状态 1-正在使用 2-空闲 3-已弃用;default:2;" json:"status"`       // 状态 1-正在使用 2-空闲 3-已弃用
	CreatedAt        time.Time `gorm:"column:created_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"created_at"`
	UpdatedAt        time.Time `gorm:"column:updated_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"updated_at"`
	DeletedAt        time.Time `gorm:"column:deleted_at;type:datetime;default:NULL;" json:"deleted_at"`
}

func (c *Cabinet) TableName() string {
	return "cabinet"
}
func (c *Cabinet) AddCabinet() error {
	return config.DB.Create(&c).Error
}
func (c *Cabinet) UpdateCabinet() error {
	return config.DB.Updates(&c).Error
}
func (c *Cabinet) CabinetList() (list []Cabinet, err error) {
	err = config.DB.Find(&list).Error
	return
}
func (c *Cabinet) CabinetDetail() error {
	return config.DB.Where("id = ?", c.Id).Find(&c).Error
}
func (c *Cabinet) GetByAddressAndSize() (list []Cabinet, err error) {
	query := config.DB
	if c.AddressId > 0 {
		query = query.Where("address_id = ?", c.AddressId)
	}
	if c.SizeId > 0 {
		query = query.Where("size_id = ?", c.SizeId)
	}
	query.Order("hot DESC").Find(&list)
	return
}
